package br.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.connection.ConnectionFactory;
import br.com.controller.AlunoController;
import br.com.controller.MatriculaController;
import br.com.utils.FormatUtils;

/**
 * Servlet implementation class CadMatriculaServlet
 */
@WebServlet("/CadMatriculaServlet")
public class CadMatriculaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadMatriculaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			doInser(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doInser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();

		Connection conn = new ConnectionFactory().getConnection();
		
		request.setCharacterEncoding("UTF-8");        
		response.setCharacterEncoding("UTF-8");
		int idcomplete = AlunoController.idalunocomplete(conn, request.getParameter("aluno"));
		int nchamada = Integer.parseInt(request.getParameter("nchamada"));
		int idaluno = idcomplete;
		String tmatri = request.getParameter("tipomatricula");
		int ensino = Integer.parseInt(request.getParameter("ensino"));
		int serie = Integer.parseInt(request.getParameter("serie"));
		int turno = Integer.parseInt(request.getParameter("turno"));
		int turma = Integer.parseInt(request.getParameter("turma"));
		int anoletivo = Integer.parseInt(request.getParameter("anoletivo"));
		String obs = request.getParameter("observacao");
		Date datacad = FormatUtils.setData(request.getParameter("datamatri"));
		

		try {

			MatriculaController.inserir(conn, tmatri, idaluno,nchamada, ensino, serie, turma, turno, anoletivo, obs, datacad);

			out.print("<script type=\"text/javascript\">	alert(\"Cadastrado com sucesso.\");</script>");
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (Exception ex) {ex.printStackTrace();}
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {}
		} 
	}
	
}
